<%@tag import="java.util.HashSet"%>
<%@tag import="java.util.Collections"%>
<%@tag import="java.util.Set"%>
<%@tag import="by.epam.communalservices.utils.Login"%>
<%@tag import="java.sql.Connection"%>

<%@ attribute name="id" required="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%
    Connection con = (Connection) request.getSession().getAttribute("currentConnection");
    Login login = new Login(con);
    login.setId( Long.valueOf(getJspContext().getAttribute("id").toString()) );

    String[][] array = new String[5][4];
    array[0][0] = "admin";
    array[1][0] = "edit";
    array[2][0] = "view";

    array[0][1] = "kadrovik";
    array[1][1] = "objecter";
    array[2][1] = "tasker";
    array[3][1] = "brigadir";

    array[0][2] = "user_view";
    array[1][2] = "object_view";
    array[2][2] = "task_view";
    array[3][2] = "team_view";
    array[4][2] = "worker_view";

    array[0][3] = "user_edit";
    array[1][3] = "object_edit";
    array[2][3] = "task_edit";
    array[3][3] = "team_edit";
    array[4][3] = "worker_edit";
    
    
   
    
    Set<String> roles = Collections.EMPTY_SET;
    if (request.getParameter("submit") != null) {
        roles = new HashSet<String>();
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != null && request.getParameter("role_" + array[i][j]) != null) {
                    roles.add(array[i][j]);
                }
            }
        }
        login.deleteRole();
        login.setRole(roles);
        System.out.println("***set: "+roles);
    } else {
        roles = login.getRole();
        System.out.println("***get: "+roles);
    }

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < array.length; i++) {
        result.append("<tr id=c>");
        for (int j = 0; j < array[i].length; j++) {
            if (j==array[i].length-1) {
	            result.append("<td id=l>");
        	} else {
	            result.append("<td>");
        	}
            if (array[i][j] != null) {
                if (roles.contains(array[i][j])) {
                    result.append("<input id=" + array[i][j] + " name=role_" + array[i][j]
                            + " type='checkbox' checked='checked' />");
                    result.append("<label for=" + array[i][j] + ">" + array[i][j] + "</label>");
                } else {
                    result.append("<input id=" + array[i][j] + " name=role_" + array[i][j] + " type='checkbox' />");
                    result.append("<label for=" + array[i][j] + " s>" + array[i][j] + "</label>");
                }
                if (request.getParameter("submit") != null) {

                }
            }
            result.append("</td>");
        }
        result.append("</tr>");
    }
    out.println(result.toString());
%>
